package com.slwu.controller;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slwu.common.RESP;
import com.slwu.entity.Order;
import com.slwu.request.order.OrderAddParam;
import com.slwu.request.order.OrderEditParam;
import com.slwu.request.order.OrderIdParam;
import com.slwu.request.order.OrderPageParam;
import com.slwu.service.IOrderService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wushaolin
 * @since 2025-10-29
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService orderService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取订单分页")
    @GetMapping("/page")
    public RESP<Page<Order>> page(OrderPageParam param) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (param.getSearchKey() != null && !param.getSearchKey().isEmpty()) {
            wrapper.lambda().like(Order::getName, param.getSearchKey());
        }
        Page<Order> page = param.createPage();
        orderService.page(page, wrapper);
        return RESP.ok(page);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("新增订单")
    @PostMapping("/add")
    public RESP<Boolean> add(@RequestBody @Validated OrderAddParam param) {
        Order order = new Order();
        order.setId(IdUtil.objectId());
        order.setName(param.getName());
        return RESP.ok(orderService.save(order));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("修改订单")
    @PostMapping("/edit")
    public RESP<Boolean> edit(@RequestBody @Validated OrderEditParam param) {
        Order order = new Order();
        order.setId(param.getId());
        order.setName(param.getName());
        return RESP.ok(orderService.updateById(order));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("订单详情")
    @GetMapping("/detail")
    public RESP<Order> detail(OrderIdParam param) {
        return RESP.ok(orderService.getById(param.getId()));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("删除订单")
    @PostMapping("/delete")
    public RESP<Boolean> delete(@RequestBody @Validated OrderIdParam param) {
        return RESP.ok(orderService.removeById(param.getId()));
    }

}
